package com.gw2.gw2world.fragment;

import com.gw2.gw2world.R;
import com.gw2.gw2world.TempleDungeonActivity;
import com.gw2.gw2world.controller.DatabaseOpenHelper;
import com.gw2.gw2world.controller.Tools;

import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

public class TempleDungeonMenuFragment extends Fragment implements OnClickListener{
	@SuppressWarnings("unused")
	private static final String TAG="TempleDungeonMenuFragment";
	private DatabaseOpenHelper dbHelper;
	protected SQLiteDatabase   db;
	private SharedPreferences  prefs;
	private Intent intent;
	private Bundle extras;
	//private static final int[] MAPS=new int[] {22,39,51,65,62}; //fireheart, maelstorm, devastation, malchor, cursed
	private SparseArray<String> map_names;
	int map_id=0;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Tools.forceLocale(getActivity());
		View v=inflater.inflate(R.layout.temple_dungeon, null);
		//UI
		v.findViewById(R.id.tv_cof) .setOnClickListener(this);
		v.findViewById(R.id.tv_coe) .setOnClickListener(this);
		v.findViewById(R.id.tv_arah).setOnClickListener(this);
		v.findViewById(R.id.tv_tob) .setOnClickListener(this);
		v.findViewById(R.id.tv_tol) .setOnClickListener(this);
		v.findViewById(R.id.tv_tod) .setOnClickListener(this);
		v.findViewById(R.id.tv_tom) .setOnClickListener(this);
		v.findViewById(R.id.tv_tog) .setOnClickListener(this);
		//other
		intent=new Intent(getActivity(), TempleDungeonActivity.class);
		extras=new Bundle();
		Tools.lockScreenFromPreferences(getActivity());
		dbHelper = new DatabaseOpenHelper(getActivity(), "database", null, 1);
		db = dbHelper.getReadableDatabase();
		prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
		Cursor c=db.query("location", null, "lang=?", new String[]{prefs.getString("language", "en")}, null, null, null);
		map_names=new SparseArray<String>(c.getCount());
		while(c.moveToNext())
			map_names.put(c.getInt(0), c.getString(2));
		return v;
	}
	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.tv_cof) { //dungeon: citadel
			extras.putInt("event_drawable", R.drawable.dungeon);
			extras.putInt("event_name", R.string.dungeon_cof);
			extras.putInt("map_id", 22);
			extras.putString("map_name", map_names.get(22));
			map_id=22;
		} else if(v.getId()==R.id.tv_coe) { //dungeon: crucide
			extras.putInt("event_drawable", R.drawable.dungeon);
			extras.putInt("event_name", R.string.dungeon_coe);
			extras.putInt("map_id", 39);
			extras.putString("map_name", map_names.get(39));
			map_id=39;
		} else if(v.getId()==R.id.tv_arah) { //dungeon: arah
			extras.putInt("event_drawable", R.drawable.dungeon);
			extras.putInt("event_name", R.string.dungeon_arah);
			extras.putInt("map_id", 62);
			extras.putString("map_name", map_names.get(62));
			map_id=51;
		} else if(v.getId()==R.id.tv_tob) { //temple: balthazar
			extras.putInt("event_drawable", R.drawable.temple);
			extras.putInt("event_name", R.string.event_temple_of_balthazar);
			extras.putInt("map_id", 51);
			extras.putString("map_name", map_names.get(51));
			map_id=65;
		} else if(v.getId()==R.id.tv_tol) { //temple: lyssa
			extras.putInt("event_drawable", R.drawable.temple);
			extras.putInt("event_name", R.string.event_temple_of_lyssa);
			extras.putInt("map_id", 65);
			extras.putString("map_name", map_names.get(65));
			map_id=62;
		} else if(v.getId()==R.id.tv_tod) { //temple: dwayna
			extras.putInt("event_drawable", R.drawable.temple);
			extras.putInt("event_name", R.string.event_temple_of_dwayna);
			extras.putInt("map_id", 65);
			extras.putString("map_name", map_names.get(65));
			map_id=62;
		} else if(v.getId()==R.id.tv_tom) { //temple: melandru
			extras.putInt("event_drawable", R.drawable.temple);
			extras.putInt("event_name", R.string.event_temple_of_melandru);
			extras.putInt("map_id", 62);
			extras.putString("map_name", map_names.get(62));
			map_id=51;
		} else if(v.getId()==R.id.tv_tog) { //temple: grenth
			extras.putInt("event_drawable", R.drawable.temple);
			extras.putInt("event_name", R.string.event_temple_of_grenth);
			extras.putInt("map_id", 62);
			extras.putString("map_name", map_names.get(62));
			map_id=51;
		}
		intent.putExtras(extras);
		startActivity(intent);
	}
}
